﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using BrokerBase.Lib.Policy;

namespace BrokerBase.Lib.Product
{
    public class AdjustRate:IAdjustRate
    {
        private IAdjustRateConverter converter;

        public AdjustRate()
        {
            Items = new List<IAdjustRateItem>();
        }

        public AdjustRate(IProduct product)
            : this()
        {
            Owner = product;
        }

        public virtual IProduct Owner { get; set; }

        public virtual string Name { get; set; }

        public virtual IList<IAdjustRateItem> Items { get; set; }

        public virtual int Id { get; set; }

        public virtual string Description { get; set; }

        public virtual string ConverterClassFullName { get; set; }

        public virtual string ConverterAssemblyFullName { get; set; }

        

        public virtual IAdjustRateConverter ConverterInstance
        {
            get
            {
                //if (string.IsNullOrEmpty(ConverterClassFullName))
                //    throw new Exception("没有关联的转换器!");

                if (converter == null && !string.IsNullOrEmpty(ConverterClassFullName))
                {
                    Assembly assembly = Assembly.LoadFrom(ConverterAssemblyFullName);

                    object obj = assembly.CreateInstance(ConverterClassFullName);

                    converter = obj as IAdjustRateConverter;
                    converter.Owner = this;

                }

                return converter;
            }
            set
            {
                converter = value;

                if (converter != null)
                {
                    converter.Owner = this;

                    Type t = converter.GetType();
                    ConverterClassFullName = t.FullName;
                    ConverterAssemblyFullName = t.Module.Name;
                }
            }
        }


        public virtual bool IsHasConverter
        {
            get { return !string.IsNullOrEmpty(ConverterClassFullName); }
        }
    }
}
